package models

import "time"

// WorkflowStatus represents workflow status
type WorkflowStatus string

const (
	WorkflowStatusActive   WorkflowStatus = "active"
	WorkflowStatusInactive WorkflowStatus = "inactive"
	WorkflowStatusDraft    WorkflowStatus = "draft"
)

// TriggerType represents trigger type
type TriggerType string

const (
	TriggerTypeManual   TriggerType = "manual"
	TriggerTypeAlert    TriggerType = "alert"
	TriggerTypeTicket   TriggerType = "ticket"
	TriggerTypeSchedule TriggerType = "schedule"
	TriggerTypeWebhook  TriggerType = "webhook"
)

// ActionType represents action type
type ActionType string

const (
	ActionTypeCreateTicket     ActionType = "create_ticket"
	ActionTypeUpdateTicket     ActionType = "update_ticket"
	ActionTypeSendNotification ActionType = "send_notification"
	ActionTypeRunScript        ActionType = "run_script"
	ActionTypeHTTPRequest      ActionType = "http_request"
	ActionTypeWait             ActionType = "wait"
	ActionTypeCondition        ActionType = "condition"
)

// ExecutionStatus represents execution status
type ExecutionStatus string

const (
	ExecutionStatusPending   ExecutionStatus = "pending"
	ExecutionStatusRunning   ExecutionStatus = "running"
	ExecutionStatusCompleted ExecutionStatus = "completed"
	ExecutionStatusFailed    ExecutionStatus = "failed"
	ExecutionStatusCancelled ExecutionStatus = "cancelled"
)

// Workflow represents a workflow definition
type Workflow struct {
	ID            string                 `bson:"_id,omitempty" json:"id"`
	Name          string                 `bson:"name" json:"name"`
	Description   string                 `bson:"description" json:"description"`
	Status        WorkflowStatus         `bson:"status" json:"status"`
	TriggerType   TriggerType            `bson:"triggerType" json:"triggerType"`
	TriggerConfig TriggerConfig          `bson:"triggerConfig" json:"triggerConfig"`
	Steps         []WorkflowStep         `bson:"steps" json:"steps"`
	Variables     map[string]string      `bson:"variables,omitempty" json:"variables,omitempty"`
	CreatedBy     string                 `bson:"createdBy" json:"createdBy"`
	CreatedAt     time.Time              `bson:"createdAt" json:"createdAt"`
	UpdatedAt     time.Time              `bson:"updatedAt" json:"updatedAt"`
}

// TriggerConfig represents trigger configuration
type TriggerConfig struct {
	// Alert trigger
	AlertSeverity string `bson:"alertSeverity,omitempty" json:"alertSeverity,omitempty"`
	AlertName     string `bson:"alertName,omitempty" json:"alertName,omitempty"`

	// Ticket trigger
	TicketPriority string `bson:"ticketPriority,omitempty" json:"ticketPriority,omitempty"`
	TicketType     string `bson:"ticketType,omitempty" json:"ticketType,omitempty"`

	// Schedule trigger
	CronExpression string `bson:"cronExpression,omitempty" json:"cronExpression,omitempty"`

	// Webhook trigger
	WebhookPath string `bson:"webhookPath,omitempty" json:"webhookPath,omitempty"`
}

// WorkflowStep represents a step in workflow
type WorkflowStep struct {
	ID              string            `bson:"id" json:"id"`
	Name            string            `bson:"name" json:"name"`
	ActionType      ActionType        `bson:"actionType" json:"actionType"`
	Parameters      map[string]string `bson:"parameters" json:"parameters"`
	DependsOn       []string          `bson:"dependsOn,omitempty" json:"dependsOn,omitempty"`
	ContinueOnError bool              `bson:"continueOnError" json:"continueOnError"`
}

// WorkflowExecution represents a workflow execution
type WorkflowExecution struct {
	ID          string                 `bson:"_id,omitempty" json:"id"`
	WorkflowID  string                 `bson:"workflowId" json:"workflowId"`
	Status      ExecutionStatus        `bson:"status" json:"status"`
	Input       map[string]string      `bson:"input,omitempty" json:"input,omitempty"`
	Output      map[string]string      `bson:"output,omitempty" json:"output,omitempty"`
	Steps       []StepExecution        `bson:"steps" json:"steps"`
	ErrorMsg    string                 `bson:"errorMsg,omitempty" json:"errorMsg,omitempty"`
	StartedAt   time.Time              `bson:"startedAt" json:"startedAt"`
	CompletedAt *time.Time             `bson:"completedAt,omitempty" json:"completedAt,omitempty"`
}

// StepExecution represents a step execution
type StepExecution struct {
	StepID      string                 `bson:"stepId" json:"stepId"`
	StepName    string                 `bson:"stepName" json:"stepName"`
	Status      ExecutionStatus        `bson:"status" json:"status"`
	Output      map[string]string      `bson:"output,omitempty" json:"output,omitempty"`
	ErrorMsg    string                 `bson:"errorMsg,omitempty" json:"errorMsg,omitempty"`
	StartedAt   time.Time              `bson:"startedAt" json:"startedAt"`
	CompletedAt *time.Time             `bson:"completedAt,omitempty" json:"completedAt,omitempty"`
}

// WorkflowTemplate represents a workflow template
type WorkflowTemplate struct {
	ID               string            `bson:"_id,omitempty" json:"id"`
	Name             string            `bson:"name" json:"name"`
	Description      string            `bson:"description" json:"description"`
	Category         string            `bson:"category" json:"category"`
	TriggerType      TriggerType       `bson:"triggerType" json:"triggerType"`
	TriggerConfig    TriggerConfig     `bson:"triggerConfig" json:"triggerConfig"`
	Steps            []WorkflowStep    `bson:"steps" json:"steps"`
	DefaultVariables map[string]string `bson:"defaultVariables,omitempty" json:"defaultVariables,omitempty"`
}
